package top.overstep.zboot.system.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.overstep.zboot.system.domain.User;
import top.overstep.zboot.system.service.UserService;
import top.overstep.zboot.system.vo.LoginVO;

import javax.validation.Valid;
import java.security.Security;
import java.util.List;

@RestController
public class PassportController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(@RequestBody(required=true) @Validated LoginVO login,Errors errors){
        if(errors.hasErrors()){
            List<ObjectError> allErrors = errors.getAllErrors();
            ObjectError objectError = allErrors.get(0);
            objectError.getObjectName();
            objectError.getDefaultMessage();
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token  =new UsernamePasswordToken();
        if (!subject.isAuthenticated()){
            subject.login(token);
        }
        return null;
    }

    @PostMapping
    public String register(User user){
        return null;
    }


    public static void main(String[] args) {
        SimpleHash simpleHash =new SimpleHash("md5","123456",ByteSource.Util.bytes("salt"),4);
//        simpleHash.setIterations(4);
//        simpleHash.setSalt();
        simpleHash.toString();
    }
}
